/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.generator;

/**
 *
 * @author Laurent WOUTERS
 */
public class ReflectionPackage extends PackageGenerator {
    private ClassGenerator classThing;
    private ClassGenerator classClass;

    public ClassGenerator getClassClass(Application app) {
        if (classClass != null)
            return classClass;
        xowl.lang.runtime.Entity entity = modelGenerator.getRepository().resolveEntity(ModelGenerator.owlClass);
        xowl.lang.runtime.Class c = modelGenerator.getRepository().interpretAsClass(entity);
        addClassClass(app, c);
        return classClass;
    }
    
    public ReflectionPackage(Application app, ModelGenerator model, xowl.lang.owl2.Ontology ontology) {
        super(app, model, ontology, "owl");
    }
    @Override public void loadEntities(Application app) {
        for (xowl.lang.runtime.Entity entity : ontology.getAllContains()) {
            for (xowl.lang.runtime.Interpretation interpretation : entity.getAllInterpretedAs()) {
                if (interpretation instanceof xowl.lang.runtime.Class) {
                    xowl.lang.runtime.Class c = (xowl.lang.runtime.Class)interpretation;
                    String iri = entity.getHasIRI().getHasValue();
                    if (iri.endsWith("#Thing"))
                        addClassThing(app, c);
                    else if (iri.endsWith("#Class"))
                        addClassClass(app, c);
                    else
                        classes.put(c, new ClassGenerator(app, this, c));
                } else if (interpretation instanceof xowl.lang.runtime.Property) {
                    xowl.lang.runtime.Property prop = (xowl.lang.runtime.Property)interpretation;
                    properties.put(prop, new PropertyGenerator(app, this, prop));
                }
            }
        }
    }
    
    private void addClassThing(Application app, xowl.lang.runtime.Class c) {
        classThing = new OWLThing(app, this, c);
        classes.put(c, classThing);
    }
    private void addClassClass(Application app, xowl.lang.runtime.Class c) {
        classClass = new OWLClass(app, this, c);
        classes.put(c, classClass);
    }

    @Override public void write(String folder, GenerationMode mode, int signature) throws java.io.IOException { }

    private static class OWLThing extends ClassGenerator {
        public OWLThing(Application app, PackageGenerator pack, xowl.lang.runtime.Class c) { super(app, pack, c); }
        @Override public String getJavaName() { return "java.lang.Object"; }
    }

    private static class OWLClass extends ClassGenerator {
        public OWLClass(Application app, PackageGenerator pack, xowl.lang.runtime.Class c) { super(app, pack, c); }
        @Override public String getJavaName() { return "java.lang.Class"; }
    }
}
